package org.electrical.controller.functionality;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.electrical.dao.EleServiceDao;
import org.electrical.pojo.EleService;
import org.electrical.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/service")
@CrossOrigin(value = "*")
public class EleServiceController {

    @Autowired
    private EleServiceDao eleServiceDao;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<EleService>> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, EleService condition) throws Exception {

        Page<EleService> pager = PageHelper.startPage(page, limit);
        List<EleService> list = eleServiceDao.getConditionList(condition);
        return new ResponseResult(pager.getTotal(), list);

    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(EleService eleService) throws Exception {

        eleServiceDao.add(eleService);
        return new ResponseResult(0,"添加成功");

    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(int serviceId) throws Exception {

        eleServiceDao.delete(serviceId);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/addDistriId")
    @ResponseBody
    public ResponseResult addDistriId(EleService eleService) throws Exception {

        eleServiceDao.addDistriId(eleService);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/addHandleId")
    @ResponseBody
    public ResponseResult addHandleId(EleService eleService) throws Exception {

        eleServiceDao.addHandleId(eleService);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/addFeedback")
    @ResponseBody
    public ResponseResult addFeedback(EleService eleService) throws Exception {

        if (eleService != null) {
            if (eleService.getSatisfied() < 3) {
                eleServiceDao.updateState(eleService.getServiceId());
                return new ResponseResult(0, "服务满意度未达标准，请重新处理！");
            }
        }
        eleServiceDao.addFeedback(eleService);
        return new ResponseResult(0, "操作成功");

    }

    @RequestMapping("/byType")
    @ResponseBody
    public ResponseResult byType() throws Exception {

        List<EleService> list = eleServiceDao.getByType();
        return new ResponseResult(list);

    }

}
